<template>
	<view>
		<web-view v-if="os=='h5'" :src="url" @message="onMessage" :plus="plus"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: '',
				urls: '',
				os: "h5",
				webview: '',
				currentWebview: '',
				plus: {
					android: {
						hardwareAccelerated: true,
						useDefaultIndicator: true,
						// 添加以下两个参数即可
						supportMultipleWindows: true,
						setSupportMultipleWindows: true,
					},

				},
			} 
		},
		onShow() {
		},
		onLoad(option) {
			if(option.url != undefined){
				this.url = option.url
				this.surl = option.url
				
			}else{
				this.getWebUrl()
				
			}
			
		},
		onUnload() {
			// #ifdef APP-PLUS
			this.webview.hide();
			// #endif
			
		},
		onHide() {
			// #ifdef APP-PLUS
			this.webview.hide();
			// #endif
		},
		onBackPress() {
			var _this = this
			console.log('返回')
			_this.currentWebview = _this.$mp.page.$getAppWebview().children()[0]; //获取当前页面的webview对象
			_this.currentWebview.canBack(function(evt) {
				// 有回退的页面
				console.log(evt)
				history.back();	
				
			});
			_this.currentWebview.back();



			return true; // 阻止返回 不阻止就不写或者用uni.navigateBack({ delta: 1 })返回
		},

		methods: {
			 onMessage(e) {
			
			        console.log(e.detail)
			
			      },
			getWebUrl() {
				var _this = this;
				this.$httpSend({
					path: '/middle.Middle/get_list',
					success: function(res) {
						if (res.length) {
							res.forEach(item => {
								if (item.status == 1) {
									_this.url = item.url;
									return
								}
							})
						}
					}
				})

			},
		},

		onNavigationBarButtonTap(e) {
			var _this = this
			//console.log(e.index)

			console.log(e.index)
			switch (e.index) {
				case 0:
					this.$httpSend({
						path: '/get/kefu',
						success(data) {
							//console.log('客服数据', data)
							uni.navigateTo({
								'url': '/pages/chat/message?list_id=' + data
							})
						}
					});
					break;
				case 1:


					// #ifdef APP-PLUS
					let currentWebview = this.$mp.page.$getAppWebview().children()[0]; //获取当前页面的webview对象
					currentWebview.reload();
					// #endif
					// #ifndef APP-PLUS
					location.reload();
					// #endif
					break;
				case 2:
				// #ifdef APP-PLUS
				_this.currentWebview = _this.$mp.page.$getAppWebview().children()[0]; //获取当前页面的webview对象
				_this.currentWebview.canBack(function(evt) {
					// 有回退的页面
					console.log(evt)
					history.back();	
					
				});
				_this.currentWebview.back();
				// #endif
				// #ifndef APP-PLUS
				history.back();	
					// #endif
				
					break;
				case 3:
					_this.url = ''
					if(_this.urls == ''){
						_this.getWebUrl();
					}else{
						_this.url = _this.urls
					}
					
					break;
				default:
					break;
			}
		},
	}
</script>

<style>
	.custom-webview {
		flex: 1;
	}
</style>